# -*- coding: utf-8 -*-
import multiprocessing
import os.path
import urllib
import requests
from lxml import etree

# 获取lieqishi推荐fuli照片

s = requests.Session()
root_dir = r'D:\work\project\spiders\lieqishi\result'


def print_process(n, max_count, text):    
    print('\rdownloading(%d/%d) %s ...' % (n, max_count, text), end='')


def do_one_page(data):
    img, lock, counter, max_count = data
    img_url, titile = img
    with lock:
        counter.value += 1
    
    print_process(counter.value, max_count, img_url)

    try:
        img_dir = os.path.join(root_dir, titile)
        if not os.path.exists(img_dir):
            os.makedirs(img_dir)
        fullname = os.path.join(img_dir, os.path.basename(img_url))
        if os.path.exists(fullname) and os.path.getsize(fullname) != 0:
            return
        with open(fullname, 'wb') as fp:
            fp.write(requests.get(img_url, timeout=10).content)
    except:
        pass


def save_file(fullname, content):
    with open(fullname, 'wb') as fp:
        fp.write(content)


def get_all_img_url():
    result = []
    # 登陆网站
    url_login = 'https://lieqishi.com/index.php/api/ulog/login'
    r = s.get(url_login, params={'username': 'style7en', 'userpass': 'zhangjia'})
    
    url_tuijian = 'https://lieqishi.com/index.php/fuli/'
    # 获取福利页推荐
    r = s.get(url_tuijian)
    tree = etree.HTML(r.content)

    url_lists = tree.xpath('//a[@class="wrap"]/@href')
    picon_lists = tree.xpath('//a[@class="wrap"]/div[@class="picon"]/text()')
    for url, picon in zip(url_lists, picon_lists):
        if picon != '0猎币':
            continue

        r = s.get(url)
        tree = etree.HTML(r.content)

        # 标题
        titile = tree.xpath('//h1[@class="header-title"]/text()')[0]

        # 第一张图片    
        mains = tree.xpath('//div[@id="content"]/img/@src')
        result.extend(map(lambda x: (x, titile), mains))

        # 隐藏图片
        # cscms_content = tree.xpath('//div[@id="content"]/script/@src')[0] # 直接写地址更快        
        _id = url.split('/')[-1]
        cscms_content = 'https://lieqishi.com/index.php/fuli/show/pay/{0}'.format(_id) 
        html = etree.HTML(urllib.parse.unquote(s.get(cscms_content).text).split("'")[1])
        result.extend(map(lambda x: (x, titile), html.xpath('//img/@src')))
    
    return result
    
    
if __name__ == '__main__':
    if not os.path.exists(root_dir):
        os.makedirs(root_dir)
    multiprocessing.freeze_support()
    pool = multiprocessing.Pool(max(multiprocessing.cpu_count() - 1, 1))
    manager = multiprocessing.Manager()
    counter = manager.Value('i', 0)
    lock = manager.Lock()

    urls = get_all_img_url()
    max_count = len(urls)
    inputs = map(lambda x: (x, lock, counter, max_count), urls)

    pool.map(do_one_page, inputs)
    pool.close()
    pool.join()
